package com.littleken.dao;

import com.littleken.entity.Users;
import com.littleken.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.List;

public class UsersDAO {
    private SqlSession sqlSession ;
    private List<Users> list;
    private Users user;

    /**
     * 获得sqlSession
     * @return
     */
    private SqlSession getSqlSession(){
        sqlSession = SqlSessionFactoryUtils.getSqlSessionFactory().openSession();
        return  sqlSession;
    }
    /**
     * 查询全部用户
     * @return
     */
    public List<Users> findAll(){
        try{
            list = getSqlSession().selectList("findUsers");
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            sqlSession.close();
        }

        return list;
    }

    /**
     * 根据Id查询指定用户数据
     * @return
     */
    public Users findById(Integer id){
        try{
        user = getSqlSession().selectOne("findUsers",new Users(id));
        }catch (Exception e){

        }finally {
            sqlSession.close();
        }
        return user;
    }
}
